<template>
  <div :class="isMe ? 'chat-msg-item is-me' : 'chat-msg-item'">
    <div class="chat-item-avatar">
      <img :src="senderInfo?._avatar" v-lazy alt="">
    </div>
    <div class="chat-item-box">
      <div class="chat-item-user-info">
        <div class="user-name"> 发送人:{{ senderInfo?._name }} 发送时间:{{ timeFormat(data.timestamp) }}</div>
      </div>
      <div class="chat-item-content">
        {{ data.message }}
      </div>
    </div>
  </div>

</template>

<script setup lang="ts">
import type { Ref } from "vue";

import { timeFormat } from "@/utils/utils.ts";
import { initUserInfo } from "@/components/ChatBox/Hooks";

const props = defineProps<{
  data: msgTableItemType
}>();
const { data } = toRefs(props);
const senderInfo: Ref<userInfoType | undefined> = ref(undefined);
const isMe: Ref<boolean> = ref(false);

initUserInfo(data.value.sender, senderInfo, isMe);


</script>

<style scoped lang="less">
.chat-msg-item {
  display: flex;
  width: 100%;
  padding-bottom: 20px;

  .chat-item-avatar {
    width: 38px;
    height: 38px;

    img {
      width: 100%;
      height: 100%;
      cursor: pointer;
      -webkit-user-select: none;
      -moz-user-select: none;
      user-select: none;
      border-radius: 50%;
      -webkit-user-drag: none;
    }
  }

  .chat-item-box {
    flex: 1;
    padding: 0 12px;

    .chat-item-user-info {
      display: flex;
      -webkit-column-gap: 4px;
      -moz-column-gap: 4px;
      column-gap: 4px;
      align-items: center;
      margin-bottom: 8px;
      font-size: 12px;
      color: var(--font-light);
      white-space: nowrap;
    }

    .chat-item-content {
      position: relative;
      width: fit-content;
      min-height: 1em;
      padding: 8px 12px;
      font-size: 15px;
      line-height: 22px;
      color: var(--font-main);
      word-break: break-word;
      white-space: pre-line;
      background-color: var(--background-2);
      border-radius: 2px 18px 18px;
    }
  }

  &.is-me {
    flex-direction: row-reverse;

    .chat-item-user-info {
      justify-content: flex-end;
    }

    .chat-item-content {
      background-color: var(--color-primary);
      margin-left: auto;
      border-radius: 18px 2px 18px 18px;
    }
  }
}
</style>
